# coding: utf-8
import json

from flask import Response, Blueprint, current_app
from flask_restful import Resource

from app.AlchemyEncoder import AlchemyEncoder


class imageApi(Resource):

    def get(self):
        # result = {};
        # result["message"] = "ok"
        # result["title"] = "hello"
        result = []
        arr = []
        f = open("f://xy.txt", "r", encoding="utf-8")
        line = f.readline()
        line = line[:-1]
        while line:
            line = f.readline()
            if len(line.split(",")) == 3:
                name, x, y = line.split(",")
                d = {"coord": [float(x), float(y)], "elevation": 10, "name": name}
                line = line[:-1]
                arr.append(d)
        result.append(arr)
        return Response(json.dumps(result, ensure_ascii=False, cls=AlchemyEncoder),
                        mimetype='application/json')


map = Blueprint('map', __name__)


@map.route('/')
def admin_index():
    print(123)
    return current_app.send_static_file('index.html')


def init_app(api):
    api.add_resource(imageApi, '/ai')


def regist_blueprint(app):
    app.register_blueprint(map, url_prefix='/map')
